
* redraw firms weights from the distributionof weights for firms in the same country. 

* redraws new firm_id (within home country)
use ${monte_data_dir}/bvd_hid_list_firmweightdomestic_`rname'.dta, clear
bys country_shr_1995 firm_id: gen firm_id = runiformint(first_id,last_id) 

drop firm_id_og first_id last_id country_shr_1995
expand 17
bys BvD: gen year=1994+_n

* merge with firm-level vars, created in input_permute.do
mmerge BvD year using ${monte_data_dir}/bvd_year_firmdata_firmweightdomestic_`rname'.dta
drop _m

* merge the macro variables 
mmerge firm_id year using ${monte_data_dir}/bvd_year_macrodata_firmweightdomestic_`rname'.dta, unmatched(master)
drop _m

* Prepare regressions:
* ---------------------

local yy=1995

clonevar LSW = lswMPm_`yy'_a
clonevar HSW = hswMPm_`yy'_a
clonevar VAEMP = vaempMPm_`yy'_a
clonevar GDPGAP = lngdpgap_`yy'_a
clonevar GDPPC = gdppcMPm_`yy'_a

clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_`yy'_a
clonevar VAEMP_foreign = vaempMPm_shr4_foreign_`yy'_a
clonevar GDPGAP_foreign = lngdpgap_shr_foreign_`yy'_a
clonevar LSW_foreign = lswMPm_shr4_foreign_`yy'_a
clonevar HSW_foreign = hswMPm_shr4_foreign_`yy'_a

clonevar spilloversown = spill${depvar}${ttt}_`yy'_a
clonevar spilloversother = spillN${depvar}${ttt}_`yy'_a
clonevar spilloversownzero = spill${depvar}${ttt}_`yy'_a0
clonevar spilloversotherzero = spillN${depvar}${ttt}_`yy'_a0
clonevar stockown = k${depvar}_${ttt} 
clonevar stockownzero = k${depvar}_${ttt}0  
clonevar stockother = kNOT_${depvar}_${ttt} 
clonevar stockotherzero = kNOT_${depvar}_${ttt}0

sort BvD year
egen lse_id = group(BvD)
bys lse_id : egen _total_${depvar}_${ttt}_`yy' = sum(${depvar}_${ttt}) if year>=`yy'+2 & year <= 2009+2
bys lse_id : egen total_${depvar}_${ttt}_`yy' = max(_total_${depvar}_${ttt}_`yy')
sort lse_id year
tab year, gen(YY_)
egen yearctry = group(year country_shr_1995) if year <= 2009
egen yearindustry = group(year industry) if year <= 2009
xtset lse_id year
sort lse_id year